package com.attedu.jdbc.homework2;

import com.attedu.jdbc.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author tune_the last
 * @date 2025/11/4 - 19:08
 */
public class Login {
    public static void main(String[] args) {
        try(Connection connection = DBUtil.getConnection()) {
            String sql = "SELECT id FROM user WHERE username = ? AND PASSWORD = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            Scanner in = new Scanner(System.in);
            System.out.print("请输入用户名：");
            ps.setString(1, in.nextLine());
            System.out.print("请输入密码：");
            ps.setString(2, in.nextLine());
            ResultSet resultSet = ps.executeQuery();
            if (resultSet.next()){
                System.out.println("登录成功！");
            }else {
                System.out.println("用户名和密码输入错误！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
